class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :auth!

  protected

  def auth!
    company = Company.find(params[:c])
    raise '公司不存在' if company.blank?
    raise '无效的用户，无须授权' if params[:current_member_id].blank?
    raise '未发起授权' if params[:code].blank?
    wx_client = WeixinAuthorize::Client.new(company.appid, company.appkey, redis_key: "wx_auth_#{company.id}")
    logger.info "::已获取code"
    sns_info = wx_client.get_oauth_access_token(params[:code])
    logger.info "::获取用户信息"
    $redis.set("wx_#{params[:c]}_#{params[:current_member_id]}_open_id", sns_info.result["openid"])
  end
end
